package app.fugema.shop.main.center.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.view.base.BaseFragment;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.viewmodel.BaseViewObserver;
import com.jeremyliao.liveeventbus.LiveEventBus;

import app.fugema.shop.R;
import app.fugema.shop.constants.AppConstants;
import app.fugema.shop.databinding.OrderListAcBinding;
import app.fugema.shop.main.center.model.GiftOrderCountResult;
import app.fugema.shop.main.center.vm.JiFenOrderVM;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.lifecycle.Observer;

public class GiftOrdersActivity extends BaseTitleActivity<OrderListAcBinding, JiFenOrderVM>
{
    //积分订单列表
    /**
     * @param context
     * @param source  0全部 2:待发货   3待收货 5已完成 (不确定)1:待付款 2:待发货 3:待收货 4:待评价
     */
    public static void start(Context context, int source)
    {
        Intent intent = new Intent(context, GiftOrdersActivity.class);
        intent.putExtra("source", source);
        context.startActivity(intent);
    }

    //订单状态 0全部 2:待发货   3待收货 5已完成
    private enum Item {

        all("全部", GiftOrderListFragment.getInstance(0)),
        //home("待付款", GiftOrderListFragment.getInstance(1)),
        send("待发货", GiftOrderListFragment.getInstance(2)),
        receive("待收货", GiftOrderListFragment.getInstance(3)),
        complete("已完成", GiftOrderListFragment.getInstance(5));

        public String name;
        public BaseFragment fragment;

        Item(String name, BaseFragment fragment)
        {
            fragment.setTitle(name);
            this.name = name;
            this.fragment = fragment;
        }

    }

    @Override
    protected void initNotificationReceipt()
    {
        super.initNotificationReceipt();
        //确认收货成功  跳转到 待评价  并弹出评价
        LiveEventBus.get(AppConstants.EventKey.ORDER_RECEIPT, String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String o)
            {
                binding.vpPager.setCurrentItem(3);
            }
        });

    }

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_list_ac);
    }


    private int source;//跳转

    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("积分订单",R.color.white);
        source = getIntent().getIntExtra("source", 0);
        binding.vpPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return Item.values()[position].fragment;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return Item.values()[position].name;
            }

            @Override
            public int getCount()
            {
                return Item.values().length;
            }
        });

        viewUtils.initSlidingTabLayout(binding.stlTab, binding.vpPager);

        if (source < Item.values().length)
        {
            binding.vpPager.setCurrentItem(source, false);
        }
    }

    @Override
    protected void initData()
    {
        super.initData();
        String userKey = LoginUtils.getToken();
        // 先查 订单数量
        viewModel.getGiftOrderCount(userKey).observe(this, new BaseViewObserver<LiveDataWrapper<GiftOrderCountResult>>()
        {
            @Override
            protected void onSuccess(LiveDataWrapper<GiftOrderCountResult> data)
            {
                //未处理
            }
        });
    }
}
